package freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.HashMap;
import java.util.Map;


public class FreemarkerDemo {

    private static final String FREEMARKER_PATH = "src/main/java/freemarker";
    private static final String TEMPLATE_PATH = FREEMARKER_PATH + "/module";
    private static final String OUT_PATH = FREEMARKER_PATH + "/output";

    public static void main(String[] args) {

        // System.out.println(System.getProperty("user.dir"));
        //C:\develop\project\zuche\relax
        // String position = System.getProperty("user.dir")+packagePosition;
        // step1 创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            // step2 获取模版路径
            configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
            // step3 创建数据模型
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("classPath", "test");
            dataMap.put("className", "User");
            dataMap.put("requestUrl", "test");
            dataMap.put("serviceId", "testId");
            dataMap.put("serviceDesc", "测试案例");
            // step4 加载模版文件
            Template template = configuration.getTemplate("\\module\\Adapter.ftl");
            // step5 生成数据
            File docFile = new File(OUT_PATH + "\\" + "User.java");
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            // step6 输出文件
            template.process(dataMap, out);
            System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^User.java 文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

}
